package make

import (
	"fmt"
	"github.com/spf13/cobra"
	"os"
	"strings"
	"wskyygyso/api/pkg/console"
)

var CmdMakeController = &cobra.Command{
	Use:   "apicontroller",
	Short: "Create api controller，exmaple: make apicontroller v1/user",
	Args:  cobra.ExactArgs(1),
	Run:   runMakeController,
}

func runMakeController(cmd *cobra.Command, args []string) {
	array := strings.Split(args[0], "/")
	if len(array) != 2 {
		console.Exit("api controller name format: v1/user")
	}
	apiVersion, name := array[0], array[1]
	model := makeModelFromString(name)
	dir := fmt.Sprintf("app/http/controllers/api/%s/%s/", apiVersion, model.PackageName)
	os.MkdirAll(dir, os.ModePerm)
	filePath := fmt.Sprintf(dir+"%s_controller.go", model.PackageName)
	createFileFromStub(filePath, "apiController", model)
}
